﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Framework.Core.Queue
{
    public class BaseStatusQueuer<TKey, TValue, TStatus> : IStatusQueuer<TKey, TValue, TStatus>
    {
        private DateTime inQueueTime;
        private KeyValuePair<TKey, TValue> keyValue;

        private DateTime lastQueryTime;
        private TStatus status;

        public BaseStatusQueuer(TKey key, TValue value, TStatus status)
        {
            keyValue = new KeyValuePair<TKey, TValue>(key, value);
            this.status = status;
        }

        #region IStatusQueuer<TKey,TValue,TStatus> Members

        public event QueuerStatusChangedHandler<TKey, TValue, TStatus> OnQueuerStatusChanged;

        public event QueuerQueriedHandler<TKey, TValue, TStatus> OnQueuerQueried;

        public TKey Key
        {
            get { return keyValue.Key; }
        }

        public TValue Value
        {
            get { return keyValue.Value; }
        }

        public TStatus Status
        {
            get { return status; }
            set
            {
                if (!Status.Equals(value))
                {
                    status = value;
                    if (!Equals(OnQueuerStatusChanged, null))
                    {
                        OnQueuerStatusChanged(this, Status);
                    }
                }
            }
        }

        public DateTime InQueueTime
        {
            get { return inQueueTime; }
        }

        public DateTime LastQueryTime
        {
            get { return lastQueryTime; }
            set
            {
                if (!LastQueryTime.Equals(value))
                {
                    lastQueryTime = value;
                    if (!Equals(OnQueuerQueried, null))
                    {
                        OnQueuerQueried(this, LastQueryTime);
                    }
                }
            }
        }

        public void OnInQueuer()
        {
            inQueueTime = DateTime.Now;
            lastQueryTime = DateTime.Now;
        }

        #endregion

        public override string ToString()
        {
            var builder = new StringBuilder();
            builder.Append('[');
            if (Key != null)
            {
                builder.Append(Key.ToString());
            }
            builder.Append(", ");
            if (Value != null)
            {
                builder.Append(Value.ToString());
            }
            builder.Append(", ");
            if (Status != null)
            {
                builder.Append(Status.ToString());
            }
            builder.Append(']');
            return builder.ToString();
        }
    }
}